
#include "./xs-lib/xs.h"
#include "./xs-lib/xs_fifo.h"
#include "./xs-lib/xs_time.h"
#include "./xs-lib//xs_pipe.h"
#include "./xs-lib/xs_thread_manage.h"
#include "./xs-lib/xs_buffer.h"
#include "./xs-lib/xs_zlog.h"


void say_hello(void)
{
    xs_logd("Hello the crule world ");
}
int xs_timer_test(void)
{
    int module = 10;
    int timer_id = 0;

    xs_timer_init();

    timer_id = xs_timer_create(module, ETIMER_LOOP);

    if(ETIMER_PROC_ERR == timer_id)
        xs_logd("Can not create timer");

    xs_timer_start(timer_id, 1000, &say_hello,0, NULL);

    while(1)
    {
        xs_timer_proc(NULL);
    }

    xs_timer_stop(timer_id);
    xs_timer_delete(timer_id);

    return 0;
}

int xs_stack_test(void)
{
    char *ptr[10] = { "hello","how", "are", "you", "?"};
    int i = 0;
    char * pop;
    xs_stack_t *new_stack = NULL;
    new_stack = xs_stack_new(0);
   // printf("max = %d  sp = %d  size = %d itlr = %d\n", (int)new_stack ->max, (int)new_stack ->sp, (int)new_stack ->size, (int)new_stack ->iter);

    for(i = 0; i < 5; i++)
    {
        xs_logd("%s", ptr[i]);
        xs_stack_push(new_stack, ptr[i]);
    }
#if 1
    for(i = 0; i < 5; i++){
        pop = xs_stack_pop(new_stack);
        xs_logd("i = %d  sp = %d  size = %d, %s", i, (int)new_stack->sp, (int)new_stack ->size, pop);
    }
#else
    pop = xs_stack_pop(new_stack);
    printf("----%s\n", pop);
    pop = xs_stack_pop(new_stack);
    printf("----%s\n", pop);
    pop = xs_stack_pop(new_stack);
    printf("----%s\n", pop);
    pop = xs_stack_pop(new_stack);
    printf("----%s\n", pop);
    pop = xs_stack_pop(new_stack);
    printf("----%s\n", pop);
#endif

    xs_stack_delete(new_stack, XS_FREE_FUNCTION);

    return 0;
}


int xs_system_test(void)
{
    int memry = xs_get_memory_info();
    int cpu = xs_get_cpu_info();
    int task = xs_get_tasks_info( );

    xs_logd("memory : %d, cpu : %d, task : %d", memry, cpu, task);
    return 0;
}


int xs_msgquen_test(void)
{
    MQUE_RET ret;
    char *msg= "Hello the crule world\n";
    char *send_msg;
    void *recv_buff = NULL;
    unsigned short int msglen = 0;
    int i = 0;

    send_msg = (char *)xs_malloc(xs_strlen(msg) + 1);
    xs_memcpy(send_msg, msg, xs_strlen(msg));
    *(send_msg + xs_strlen(msg)) = '\0';

    ret = xs_msgque_init();
    if(MQUE_RET_OK != ret){
        xs_logd("msgque init error\n");
        goto end;
    }

    xs_logd("net_recv: %ld  net_send: %ld  ppc_recv: %ld ppc_send: %ld\n", netrecv_id, netsend_id, ppcrecv_id, ppcsend_id);

    xs_msgque_send(netsend_id, (void*)send_msg, xs_strlen(send_msg));

    xs_msgque_recv(netsend_id, (void **)&recv_buff,&msglen);
    for(i = 0; i < msglen; i++)
        printf("%c ", *((char *)(recv_buff + i)));
    xs_logd("\n");
    xs_free(recv_buff);
end:
    return 0;
}

void xs_md5_test(void)
{
    unsigned char digest[16];  //存放结果

    //第一种用法:

    MD5_CTX md5c;
    MD5Init(&md5c); //初始化
    MD5UpdaterString(&md5c,"你要测试的字符串");
    MD5FileUpdateFile(&md5c,"你要测试的文件路径");
    MD5Final(digest,&md5c);

    //第二种用法:
    MDString("你要测试的字符串",digest); //直接输入字符串并得出结果
    xs_logd("%s", digest);
    //第三种用法:
    MD5File("你要测试的文件路径",digest); //直接输入文件路径并得出结果
    xs_logd("%s", digest);
}

void fifo_test()
{
    struct fifo *root;
    struct fifo *N,*P,*Q;

    root = xs_fifo_init();
#if 0
    /* Next and prev point to the same address when call xs_fifo_init function */
    xs_logd("root: %p, root->next: %p, root->prev: %p", root, root->next, root->prev);

    xs_logd("-------------------------------------------------------------------------");
#endif
    N = xs_fifo_init();
    xs_fifo_add(root, N);
#if 0
    xs_logd("root: %p, root->next: %p, root->prev: %p", root, root->next, root->prev);
    xs_logd("N: %p, N->next: %p, N->prev: %p",
            N, N->next, N->prev);

    xs_logd("-------------------------------------------------------------------------");
#endif
    P = xs_fifo_init();
    xs_fifo_add(root, P);
#if 0
    xs_logd("root: %p, root->next: %p, root->prev: %p",
            root, root->next, root->prev);
    xs_logd("N: %p, N->next: %p, N->prev: %p",
            N, N->next, N->prev);
    xs_logd("P: %p, P->next: %p, P->prev: %p",
            P, P->next, P->prev);
#endif
    xs_fifo_del(N);
    xs_fifo_del(P);
#if 0
    xs_logd("-------------------------------------------------------------------------");

    xs_logd("root: %p, root->next: %p, root->prev: %p",
            root, root->next, root->prev);
    xs_logd("N: %p, N->next: %p, N->prev: %p",
            N, N->next, N->prev);
    xs_logd("P: %p, P->next: %p, P->prev: %p",
            P, P->next, P->prev);
#endif

    xs_fifo_del(root);
}

#include "./xs-lib/xs_linklist.h"
struct list *list;
void list_node_test()
{
    list = list_new();

}

#define CF_END 2
#define CF_CHILD 0
#define CF_PARENT 1
int xs_pipe_test (void)
{
    pid_t pid;
    int xs_pipe[CF_END];
    const char *message = "hello, the world !\n";
    char messbuf[256] = {0};

    /*@group*/
    /* Create the pipe.  */
    if (pipe (xs_pipe))
    {
        fprintf (stderr, "Pipe failed.\n");
        return EXIT_FAILURE;
    }
    /*@end group*/

    /* Create the child process.  */
    pid = fork ();
    if (pid == (pid_t) 0)
    {
        /* This is the child process.
        Close other end first.  */
        close (xs_pipe[CF_PARENT]);
       if ( 0 < read_from_pipe (xs_pipe[CF_CHILD], messbuf, sizeof(messbuf)) )
           xs_logd("%s", messbuf);

        return EXIT_SUCCESS;
    }
    else if (pid < (pid_t) 0)
    {
        /* The fork failed.  */
        fprintf (stderr, "Fork failed.\n");
        return EXIT_FAILURE;
    }
    else
    {
        /* This is the parent process.
        Close other end first.  */
        close (xs_pipe[CF_CHILD]);
        write_to_pipe (xs_pipe[CF_PARENT], message, strlen(message));
        return EXIT_SUCCESS;
    }
}

int main(int argc, char *argv[])
{

#if 0
    xs_stack_test();
    xs_system_test();
    xs_msgquen_test();
    xs_md5_test();

    xs_timer_test();
#endif

#if 0
    int i;
    for(i = 0; i < 0xEEEEEEEE; i++)
        fifo_test();
#endif
#if 0
    struct tm timeptr;
    dumptime(&timeptr);
#endif
#if 0
    xs_pipe_test();
#endif
#if 0
    xs_thread_test();

#endif
#if 0
    xs_buffer_test(argc, argv);
#endif

#if 0
    xs_zlog_test(argv);
#endif

#if 1
xs_epoll_main();

#endif
    return 0;
}
